﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace theguineasyndrome.levels
{
    public class EndLevel : LevelLoader
    {
        private ObstacleElement door1;
        private ObstacleElement door2;
        private ObstacleElement door3;

        private int lastFountainDrop;

        private Random random;

        public EndLevel(GameMode gameMode, ContentManager content, WaterWeapon waterWeapon, FireWeapon fireWeapon, PortalWeapon portalWeapon, PaintWeapon paintWeapon)
            : base(gameMode, content, waterWeapon, fireWeapon, portalWeapon, paintWeapon)
        {
            lastFountainDrop = 0;
            random = new Random();
        }

        private void cloud1Updater(ObstacleElement cloud, GameTime gameTime)
        {
            cloud.Position = level.getPointMarkPosition("nuage plateform 1") + new Vector2(0, (float)(Math.Cos(gameTime.TotalGameTime.TotalSeconds) * 200));
            if (cloud.Character != null)
                cloud.Character.setPosition(cloud.Position + cloud.CharacterRelativePosition);
        }

        private void cloud2Updater(ObstacleElement cloud, GameTime gameTime)
        {
            cloud.Position = level.getPointMarkPosition("nuage plateform 2") + new Vector2((float)(Math.Cos(gameTime.TotalGameTime.TotalSeconds) * 200), 0);
            if (cloud.Character != null)
                cloud.Character.setPosition(cloud.Position + cloud.CharacterRelativePosition);
        }

        private void cloud3Updater(ObstacleElement cloud, GameTime gameTime)
        {
            cloud.Position = level.getPointMarkPosition("nuage plateform 3") + new Vector2(0, (float)(Math.Cos(gameTime.TotalGameTime.TotalSeconds) * 70));
            if (cloud.Character != null)
                cloud.Character.setPosition(cloud.Position + cloud.CharacterRelativePosition);
        }

        private void cloud4Updater(ObstacleElement cloud, GameTime gameTime)
        {
            cloud.Position = level.getPointMarkPosition("nuage plateform 4") + new Vector2(0, (float)(Math.Sin(gameTime.TotalGameTime.TotalSeconds) * 130));
            if (cloud.Character != null)
                cloud.Character.setPosition(cloud.Position + cloud.CharacterRelativePosition);
        }

        private void fountainUpdater(ObstacleElement fountain, GameTime gameTime)
        {
            const int fountainPeriod = 100;
            int time = (int)gameTime.TotalGameTime.TotalMilliseconds;
            if (lastFountainDrop < time - fountainPeriod)
            {
                lastFountainDrop = time;

                int numDrops = random.Next(4, 7);
                for (int i = 0; i < numDrops; i++)
                    paintWeapon.firePrimary(time, level.getPointMarkPosition("peinture"), new Vector2(-1, 3) * 0);
            }
        }

        private void switch1On(Switch switch1)
        {
            level.removeElement(door1);
            level.removeElement(door2);
            level.removeElement(door3);
        }

        public override Level load()
        {
            level = getLevel("levels/level2");

            // guinea pig
            Texture2D guineaPigTexture = content.Load<Texture2D>("images/enemies/guineapig");
            for (int i = 1; i <= 4; i++)
                level.addElement(new GuineaPig(level, guineaPigTexture, level.getPointMarkPosition("hamster sans rien " + i)));

            // knight
            Texture2D knightTexture = content.Load<Texture2D>("images/enemies/knight");
            for (int i = 1; i <= 4; i++)
                level.addElement(new Knight(level, knightTexture, level.getPointMarkPosition("ennemyKnight " + i)));

            // king
            Texture2D kingTexture = content.Load<Texture2D>("images/enemies/king");
            level.addElement(new King(level, kingTexture, level.getPointMarkPosition("ennemyKing 1")));

            // moving clouds
            Texture2D cloudTexture = content.Load<Texture2D>("images/level/level/obstacles/cloud");
            ObstacleElement cloud1 = new ObstacleElement(level, cloudTexture, level.getPointMarkPosition("nuage plateform 1"));
            cloud1.setUpdater(cloud1Updater);
            level.addElement(cloud1);

            ObstacleElement cloud2 = new ObstacleElement(level, cloudTexture, level.getPointMarkPosition("nuage plateform 2"));
            cloud2.setUpdater(cloud2Updater);
            level.addElement(cloud2);

            ObstacleElement cloud3 = new ObstacleElement(level, cloudTexture, level.getPointMarkPosition("nuage plateform 3"));
            cloud3.setUpdater(cloud3Updater);
            level.addElement(cloud3);

            Texture2D elevatorTexture = content.Load<Texture2D>("images/level/level/obstacles/elevator");
            ObstacleElement cloud4 = new ObstacleElement(level, elevatorTexture, level.getPointMarkPosition("nuage plateform 4"));
            cloud4.setUpdater(cloud4Updater);
            level.addElement(cloud4);

            // doors
            Texture2D rockTexture = content.Load<Texture2D>("images/level/level/obstacles/rocks");
            door1 = new ObstacleElement(level, rockTexture, level.getPointMarkPosition("porte 1"));
            level.addElement(door1);
            door2 = new ObstacleElement(level, rockTexture, level.getPointMarkPosition("porte 2"));
            level.addElement(door2);
            door3 = new ObstacleElement(level, rockTexture, level.getPointMarkPosition("porte 3"));
            level.addElement(door3);

            // switches
            Texture2D switchTexture = content.Load<Texture2D>("images/level/level/obstacles/switch");
            Texture2D switchBlockTexture = content.Load<Texture2D>("images/level/level/obstacles/switchblock");
            Switch switch1 = new Switch(level, switchTexture, switchBlockTexture, level.getPointMarkPosition("interupteur 1"));
            switch1.setOn(switch1On);
            level.addElement(switch1);
            switch1.addBlockToLevel();
            
            // paint fountain
            Texture2D sandwichTexture = content.Load<Texture2D>("images/level/level/scenery/sandwich");
            ObstacleElement fountain = new ObstacleElement(level, sandwichTexture, level.getPointMarkPosition("hiddenSandwich")); // LOL, only used as a timer
            fountain.setUpdater(fountainUpdater);
            level.addElement(fountain);

            addCarots(level);

            Song song = content.Load<Song>("music/Crackbrained");
            MediaPlayer.Stop();
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Play(song);

            return level;
        }

    }
}
